import asyncore
import socket
import zmq

'''
	this script start a nomal socket and zmq PUB server,
	zmq PUB server will pub the msg which recive from the socket
'''

context = zmq.Context()  
zmqSocket = context.socket(zmq.PUB)  
class EchoHandler(asyncore.dispatcher_with_send):
	def handle_read(self):
		data=self.recv(4)
		if data:
			zmqSocket.send("data"+ data )

class EchoServer(asyncore.dispatcher):

	def __init__(self,host,port,zmqPort):
		asyncore.dispatcher.__init__(self)
		self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
		self.set_reuse_addr()
		self.bind((host,port))
		self.listen(5)
		zmqSocket.bind("tcp://"+host+":"+zmqPort)  

	def handle_accept(self):
		pair=self.accept()
		if pair is not None:
			sock,addr=pair
			print 'connect from %s' % repr(addr)
			handler=EchoHandler(sock)

def startRecServer(ips,port,zmqPort):
	print 'start recServer ... '
	server=EchoServer(ips,port,zmqPort)
	asyncore.loop()
	print 'recServer end'